package cn.stylefeng.guns.modular.business.frequency.pricelibrary.listener.excel;

import cn.stylefeng.guns.modular.business.frequency.cmcompany.entity.CmCompany;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.mapper.CmCompanyMapper;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.model.export.CompanyLinkmanExport;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.entity.CmLinkman;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.mapper.CmLinkmanMapper;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author jzy
 * @Date 2021/10/28
 **/
public class CompanyLinkmanListener extends AnalysisEventListener<CompanyLinkmanExport> {

    private static final int BATCH_COUNT = 3000;
    private List<CompanyLinkmanExport> data = new ArrayList<>();
    private CmCompanyMapper companyMapper;
    private CmLinkmanMapper linkmanMapper;
    private BusinessUtil businessUtil;

    public CompanyLinkmanListener(CmCompanyMapper companyMapper,
            CmLinkmanMapper linkmanMapper,
            BusinessUtil businessUtil) {
        this.companyMapper = companyMapper;
        this.linkmanMapper = linkmanMapper;
        this.businessUtil = businessUtil;
    }

    @Override
    public void invoke(CompanyLinkmanExport companyLinkmanExport, AnalysisContext analysisContext) {
        data.add(companyLinkmanExport);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        for (CompanyLinkmanExport cle : data) {
            CmCompany company = companyMapper.getByName(cle.getCompanyName(), businessUtil.getTenantId());
            if (company == null) {
                CmCompany company1 = new CmCompany();
                company1.setName(cle.getCompanyName());
                company1.setType(cle.getCompanyType().equals("企业") ? 1 : cle.getCompanyType().equals("同行") ? 2 : 3);
                company1.setNature(cle.getCompanyNature().equals("是") ? 1 : 0);
                companyMapper.insert(company1);
                if (linkmanMapper.countByPhone(cle.getLinkmanPhone(), null, businessUtil.getTenantId()) == 0) {
                    CmLinkman linkman = new CmLinkman();
                    linkman.setCompanyId(company1.getId());
                    linkman.setName(cle.getLinkmanName());
                    linkman.setPhone(cle.getLinkmanPhone());
                    linkmanMapper.insert(linkman);
                }
            } else {
                if (linkmanMapper.countByPhone(cle.getLinkmanPhone(), null, businessUtil.getTenantId()) == 0) {
                    CmLinkman linkman = new CmLinkman();
                    linkman.setCompanyId(company.getId());
                    linkman.setName(cle.getLinkmanName());
                    linkman.setPhone(cle.getLinkmanPhone());
                    linkmanMapper.insert(linkman);
                }
            }

        }
    }
}
